/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-05-20
 * Time: 15:22
 */
class Animal{
    public String name;
    public int age;

    /*public Animal(){

    }*/

    /*static{
        System.out.println("Animal::static{}");
    }

    {
        System.out.println("Animal::{}");
    }*/

    public Animal(String name, int age){
        this.name = name;
        this.age = age;
        System.out.println("Animal(String, int)");
    }

    public void eat(){
        System.out.println(name + "吃饭");
    }

    public void function(){
        System.out.println("Animal::function()!");
    }

    public static void staticFunc(){

    }
}

class Dog extends Animal{
    // 傻狗 - 够的属性
    public boolean silly;
    //public String name = "hello";

    /*public Dog(){
        super();
    }*/

    /*static{
        System.out.println("Dog::static{}");
    }

    {
        System.out.println("Dog::{}");
    }*/

    public Dog(String name, int age, boolean silly){
        //1.先帮助父类部分初始化 必须放到第一行
        super(name, age);
        //super();
        this.silly = silly;
        System.out.println("Dog(String, int, boolean)");
    }

    public void houseGuard(){
        System.out.println(name + "正在看家护院");
        //实例内部类当中才会包含外部类的this
        //System.out.println(Animal.this.name+"正在看家护院");
        //eat();
        staticFunc();
    }

    @Override
    public void eat() {
        System.out.println(name + "在吃狗粮");
    }
}

class Cat extends Animal{

    /*static{
        System.out.println("Cat::static{}");
    }

    {
        System.out.println("Cat::{}");
    }*/

    public Cat(){
        super("CC", 10);
        //this("CC", 10);
        System.out.println("Cat()");
    }

    public Cat(String name, int age){
        super(name, age);
    }

    //不加特有属性了
    public void catchMouse(){
        System.out.println(super.name + "抓老鼠");
    }

    @Override
    public void eat() {
        System.out.println(name + "吃猫粮");
    }
}

public class Test {

    public static void main(String[] args) {
        Animal animal2 = new Dog("hello", 10, false);
        //animal2.catchMouse();
        //animal2 是不是引用了Cat这个对象
        if(animal2 instanceof Cat){
            Cat cat = (Cat)animal2;
            cat.catchMouse();
        }
    }

    public static void main4(String[] args) {
        Animal animal2 = new Cat("hello", 10);
        //animal2.catchMouse();
        Cat cat = (Cat)animal2;
        cat.catchMouse();
    }

    //2.方法的传参过程当中 也可以发生向上转型
    public static void function2(Animal animal){
        animal.eat();//多态 - 一个引用调用一个方法 因为这个引用所引用的对象不一样 导致调用的方法所表现出来的行为不一样
    }

    public static Animal function3(){
        return new Cat();
    }

    public static void main3(String[] args) {
        Dog dog = new Dog("hello", 10, false);
        function2(dog);

        Cat cat = new Cat("haha",7);
        function2(cat);

    }

    public static void main2(String[] args) {
        Dog dog = new Dog("hello", 10, false);
        //Animal这个引用 指向了Dog对象
        Animal animal = dog;
        //animal.sillt = false; 不能访问 原因：没有这个属性 此时的animal只能调用自己特有的属性
        animal.eat();//动态绑定 编译的时候 还是Animal的eat方法 但在程序运行的时候 变成了子类的

        //1.直接赋值
        Animal animal2 = new Dog("hello", 10, false);

        Animal animal3 = new Cat();

    }

    public static void main1(String[] args) {
        Dog dog = new Dog("hello", 10, false);

        System.out.println("=============");

        Dog dog2 = new Dog("hello", 10, false);

    }
}
